﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace PSE.Deployment.ConfigurationManager.Controls
{
    public class ConfigurationMessageBoxWindow : System.Windows.Window
    {
		public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(ConfigurationMessageBoxWindow), new FrameworkPropertyMetadata(null));
		public static readonly DependencyProperty MessageBoxIconProperty = DependencyProperty.Register("MessageBoxIcon", typeof(ConfigurationMessageBoxImage), typeof(ConfigurationMessageBoxWindow), new FrameworkPropertyMetadata(null));
		public static readonly DependencyProperty ButtonProperty = DependencyProperty.Register("Button", typeof(ConfigurationMessageBoxButton), typeof(ConfigurationMessageBoxWindow), new FrameworkPropertyMetadata(null));

		public string Message
		{
			get { return (string)GetValue(MessageProperty); }
		}
		public ConfigurationMessageBoxImage MessageBoxIcon
		{
			get { return (ConfigurationMessageBoxImage)GetValue(MessageBoxIconProperty); }
		}
		public ConfigurationMessageBoxButton Button
		{
			get { return (ConfigurationMessageBoxButton)GetValue(ButtonProperty); }
		}

        ConfigurationMessageBoxResult _retorno;
        public ConfigurationMessageBoxResult Retorno
        {
            get
            {
                return _retorno;
            }
        }

        public ConfigurationMessageBoxWindow(String message, String title, ConfigurationMessageBoxButton messageBoxButton, ConfigurationMessageBoxImage messageBoxImage, double? windowHeight, double? windowWidth)
        {
            this.Title = title;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            SetValue(MessageProperty, message);
            SetValue(ButtonProperty, messageBoxButton);
            SetValue(MessageBoxIconProperty, messageBoxImage);

			this.SetResourceReference(StyleProperty, "ConfigurationMessageBoxWindowStyle");
            
            if(windowHeight.HasValue)
                this.Height = windowHeight.Value;
            if(windowWidth.HasValue)
                this.Width = windowWidth.Value;

			CommandBindings.Add(new CommandBinding(ConfigurationMessageBoxCommands.Close, btnClose_Click));
            CommandBindings.Add(new CommandBinding(ConfigurationMessageBoxCommands.Cancel, btnCancel_Click));
			CommandBindings.Add(new CommandBinding(ConfigurationMessageBoxCommands.No, btnNo_Click));
			CommandBindings.Add(new CommandBinding(ConfigurationMessageBoxCommands.OK, btnOK_Click));
			CommandBindings.Add(new CommandBinding(ConfigurationMessageBoxCommands.Yes, btnYes_Click));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            Grid header = this.GetTemplateChild("GridDragMove") as Grid;

            if (header != null)
            {
                header.MouseLeftButtonDown += new MouseButtonEventHandler(header_MouseLeftButtonDown);
            }
        }

        void header_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        void btnClose_Click(object sender, RoutedEventArgs e)
        {
            _retorno = ConfigurationMessageBoxResult.None;
            this.Close();
        }

        void btnYes_Click(object sender, RoutedEventArgs e)
        {
            _retorno = ConfigurationMessageBoxResult.Yes;
            this.Close();
        }

        void btnNo_Click(object sender, RoutedEventArgs e)
        {
            _retorno = ConfigurationMessageBoxResult.No;
            this.Close();
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            _retorno = ConfigurationMessageBoxResult.Cancel;
            this.Close();
        }

        void btnOK_Click(object sender, RoutedEventArgs e)
        {
            _retorno = ConfigurationMessageBoxResult.Ok;
            this.Close();
        }
	}
}